//
//  LoginController.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/8/18.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import UIKit

enum AppState: Int {
    case ApplyState
    case MemberState
    case AdminState
    case none
}

protocol LoginDelegate: class {
    func loginSuccess(from state: AppState)
}

class LoginController: BaseVC {

    
    @IBOutlet weak var LoginTF: HDUITextField!
    @IBOutlet weak var passwordTF: HDUITextField!
    @IBOutlet weak var loginButton: TransitionButton!
    
    let queue = DispatchQueue(label: "backgroundQueue")
    let storeHelper = StoreVCHelper()
    
    var menuButton: UIBarButtonItem!
    
    weak var delegate: LoginDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if self.revealViewController() != nil {
            menuButton = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(self.revealViewController().revealToggle(_:)))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
            navigationController?.navigationItem.leftBarButtonItem = menuButton
        }
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        LoginTF.text! = ""
        passwordTF.text! = ""
    }
    
    
    @IBAction func register(_ button: UIButton!) {
        let registerVC = RegisterVC()
        registerVC.delegate = self
        navigationController?.pushViewController(registerVC, animated: true)
        //self.present(registerVC, animated: true, completion: nil)
    }
    
    @IBAction func forget(_ button: UIButton!) {
        let forgetVC = ForgetVC()
        navigationController?.pushViewController(forgetVC, animated: true)
        //self.present(forgetVC, animated: true, completion: nil)
    }
    
    
    @IBAction func login(_ button: TransitionButton!) {
        view.endEditing(true)
        guard LoginTF.text != "" else {
            LoginTF.animateLineView()
            return
        }
        guard passwordTF.text != "" else {
            passwordTF.animateLineView()
            return
        }
        
        button.startAnimation()
        helper.login(with: LoginTF.text!, password: passwordTF.text!) {  error  in
            guard error != nil else {
                User.isLogin = true
                self.storeHelper.prepareAppState { state in
                    guard state != nil else {
                        self.showError(message: "网络错误，请连接互联网!")
                        return
                    }
                    self.delegate?.loginSuccess(from: state!)
                    button.stopAnimation()
                    self.dismiss(animated: true, completion: nil)
                }
                return
            }
            if error == .wrongPassword {
                self.showError(message: "密码错误，请重试!")
            } else if error == LoginError.wrongPhone {
                self.showError(message: "找不到该用户，请先注册!")
            } else if error == LoginError.networkError {
                self.showError(message: "网络错误，请连接互联网!")
            }
            button.stopAnimation()
        }
        
    }
    

}
extension LoginController: Delegate {
    func getBackPhone(_ phone: String) {
        LoginTF.text! = phone
    }
}
